package org.edu.modules.logistics.good.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.logistics.good.dto.VeHqChurukuDto;
import org.edu.modules.logistics.good.entity.VeHqChuruku;
import org.edu.modules.logistics.good.service.VeHqChurukuService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"出入库"})
@RestController
@RequestMapping("/veHqChuruku")
@Slf4j
public class VeHqChurukuController{

    @Autowired
    private VeHqChurukuService veHouqinChurukuService;

    @ApiOperation("分页查询出入库")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeHqChuruku> page) {
        QueryWrapper<VeHqChuruku> queryWrapper =  page.buildQueryWrapper(VeHqChuruku.class);
        return Result.OK(veHouqinChurukuService.pageDto(page, queryWrapper));
    }

    @ApiOperation("分页查询库存")
    @PostMapping("/kcpage")
    public Result<?> kcpage(@RequestBody PageDto<VeHqChuruku> page) {
        QueryWrapper<VeHqChuruku> queryWrapper =  page.buildQueryWrapper(VeHqChuruku.class);
        return Result.OK(veHouqinChurukuService.kcPageDto(page, queryWrapper));
    }


    @ApiOperation("导出数据）")
    @PostMapping("/kcpageexport")
    public Result<?> kcpageexport(@RequestBody PageDto<VeHqChuruku> page, HttpServletResponse resp) {
        QueryWrapper<VeHqChuruku> queryWrapper = page.buildQueryWrapper(VeHqChuruku.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("仓库名称","ckName"));
        list.add(new ExcelPo("物品名称","wpName"));
        list.add(new ExcelPo("库存数量","number"));
        PoiUtils.downloadFile(resp,veHouqinChurukuService.kcPageDto(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeHqChuruku> page, HttpServletResponse resp) {
        QueryWrapper<VeHqChuruku> queryWrapper = page.buildQueryWrapper(VeHqChuruku.class);
        List<ExcelPo> list = new ArrayList<>();
        String viewName = (String) page.getParams().get("viewName");
        if (viewName.equals("调拨")){
            list.add(new ExcelPo("调出仓库名称", "ckName"));
            list.add(new ExcelPo("调入仓库名称", "glName"));
        }else{
            list.add(new ExcelPo("仓库名称","ckName"));
        }


        list.add(new ExcelPo("物品名称","wpName"));
        list.add(new ExcelPo(viewName+"数量","number"));
        if (viewName.equals("借用")){
            list.add(new ExcelPo("归还数量", "glNum"));
        }
        list.add(new ExcelPo(viewName+"时间","crTime"));
        list.add(new ExcelPo(viewName+"申请人","applyUserName"));
        list.add(new ExcelPo("经手人","jbUserName"));
        list.add(new ExcelPo("批准人","pjUserName"));
                PoiUtils.downloadFile(resp,veHouqinChurukuService.pageDto(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "出入库-分页列表查询")
    @ApiOperation(value="出入库-分页列表查询", notes="出入库-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeHqChuruku entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeHqChuruku> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeHqChuruku> page = new PageDto<VeHqChuruku>(pageNo, pageSize);
        IPage<VeHqChuruku> pageList = veHouqinChurukuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeHqChuruku dto = veHouqinChurukuService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeHqChurukuDto dto) {
        veHouqinChurukuService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }


    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/addList")
    public Result<?> addList(@RequestBody List<VeHqChurukuDto> dto) {
        veHouqinChurukuService.insertList(dto);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeHqChurukuDto dto) {
        veHouqinChurukuService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veHouqinChurukuService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeHqChuruku> updateWrapper = new LambdaUpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set(VeHqChuruku::getStatus,statusDto.getStatus());
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            updateWrapper.set(VeHqChuruku::getPjUser,loginUser.getId());
            updateWrapper.set(VeHqChuruku::getPjUserName,loginUser.getRealname());
            updateWrapper.in(VeHqChuruku::getId,statusDto.getIds());
            veHouqinChurukuService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }




}
